from manimlib import *
class Test(Scene):    
    def construct(self):        
    axes = NumberPlane(x_range=[-7, 7], y_range=[-3, 3], axis_config={"stroke_color": BLUE,"include_numbers":True,"stroke_width":1},background_line_style={"stroke_color":GREY,"stroke_width":0.1})        
    zerolabel = Text("0",font="SimSun",font_size=26).move_to(axes.c2p(-0.1,-0.2))        
    #self.play(FadeIn(axes),FadeIn(zerolabel))        
    info1 = Tex(r"f(x)\text{在[a,b]上连续，在(a,b)内可导,且有}|f'(x)|\leq M,f(a)=0", font="SimSun",font_size=24, color=BLUE).to_edge(UP)        
    self.play(Write(info1))        self.wait(5)        
    info2 = Tex(r"\text{证明:}\frac{2}{(a-b)^2}\bigg{|}\int_a^bf(x)\text{d}x\bigg{|}\leq M", font="SimSun",font_size=24, color=BLUE).next_to(info1,DOWN)        
    self.play(Write(info2))        
    self.wait(3)        
    DL = DashedLine(axes.c2p(-1.4,2),axes.c2p(1.6,2),color=GREEN,buff=0)        
    self.play(ShowCreation(DL))        
    self.wait(8)        
    # 拉格朗日定理画图        
    # # 自制坐标        
    line1 = Arrow(axes.c2p(0,-1.5),axes.c2p(0,1.8),color=GREY,buff=0)        
    line1_label = Tex(r"y",color=GREY,font_size=30).move_to(axes.c2p(0.2,1.7))        
    line2 = Arrow(axes.c2p(-1,-1),axes.c2p(3,-1),color=GREY,buff=0)        
    line2_label = Tex(r"x",color=GREY,font_size=30).move_to(axes.c2p(2.9,-0.8))        
    self.play(ShowCreation(line1),ShowCreation(line2),FadeIn(line1_label),FadeIn(line2_label))        
    graph = axes.get_graph(lambda x: np.sin(x)*np.cos(3*x), color=RED, x_range=[0.79, 2.5, 0.1])        
    self.play(ShowCreation(graph))        
    geaphLabel = Tex(r"f(x)",color=RED,font_size=22).move_to(axes.c2p(1.3,0.2))        
    self.play(FadeIn(geaphLabel))        
    a = Dot(axes.c2p(0.79,-0.5),color=YELLOW,radius=0.05)        
    b = Dot(axes.c2p(2.5,0.21),color=YELLOW,radius=0.05)        
    aLabel = Tex(r"a",color=YELLOW,font_size=22).next_to(a,LEFT,buff=0)        
    bLabel = Tex(r"b",color=YELLOW,font_size=22).next_to(b,UR,buff=0)        
    self.play(FadeIn(VGroup(a,b,aLabel,bLabel)))        
    ab = Line(a.get_center(),b.get_center(),color=YELLOW)        
    self.play(ShowCreation(ab))        self.wait(3)        
    line = axes.get_graph(lambda x: (0.71*x+0.09)/1.72, color=YELLOW, x_range=[0.5, 2.8, 0.1])        
    c = Dot(axes.c2p(2,0.87),color=YELLOW,radius=0.05)        
    cLabel = Tex(r"\xi",color=YELLOW,font_size=30).next_to(c,UP,buff=0.1)        
    self.play(FadeIn(c),FadeIn(cLabel))        self.play(ShowCreation(line))        
    self.wait(3)        
    tip = Tex(r"f'(\xi)=\frac{f(b)-f(a)}{b-a}",font="SimSun",font_size=30, color=YELLOW).move_to(axes.c2p(0.5,-2.5))        
    self.play(FadeIn(tip))        self.wait(3)        
    # 整体左移        
    self.play(VGroup(line1,line2,line1_label,line2_label,graph,geaphLabel,a,b,aLabel,bLabel,ab,c,cLabel,line,tip).animate.shift(LEFT*5))        self.wait(3)        
    # 证明        
    T1 = Tex(r"\text{只需证明:}\bigg{|}\int_a^bf(x)\text{d}x\bigg{|}\leq M\cdot\frac{(a-b)^2}{2}",font="SimSun",font_size=24, color=YELLOW).move_to(axes.c2p(1,1.5))        
    self.play(FadeIn(T1))        self.wait(3)        
    T2 = Tex(r"\bigg{|}\int_a^bf(x)\text{d}x\bigg{|}\leq \int_a^b\bigg{|}f(x)\bigg|\text{d}x",font="SimSun",font_size=30, color=RED).move_to(axes.c2p(1,0.5))        
    self.play(FadeIn(T2))        self.wait(3)        
    T3 = Tex(r"= \int_a^b\bigg{|}f(x)-f(a)\bigg|\text{d}x",font="SimSun",font_size=30, color=RED).next_to(T2,DOWN).shift(RIGHT*1.4)        self.play(FadeIn(T3))        
    self.wait(3)        
    SR = SurroundingRectangle(tip, color=BLUE, buff=0.2,stroke_width=1.5)        
    self.play(ShowCreation(SR))        self.wait(1)        
    T4 = Tex(r"= \int_a^b\bigg{|}f'(\xi)(x-a)\bigg|\text{d}x",font="SimSun",font_size=30, color=BLUE).next_to(T3,DOWN,aligned_edge=LEFT)        
    self.play(TransformFromCopy(T3,T4))        
    self.wait(3)
    T5 = Tex(r"\leq M\int_a^b(x-a)\text{d}x",font="SimSun",font_size=30, color=BLUE).next_to(T4,DOWN,aligned_edge=LEFT)        
    self.play(TransformFromCopy(T4,T5))
    self.wait(3) 
    T6 = Tex(r"\leq M\int_a^b(x-a)\text{d}(x-a)",font="SimSun",font_size=30, color=BLUE).next_to(T4,DOWN,aligned_edge=LEFT)        
    self.play(Transform(T5,T6))        
    self.wait(3)        
    T7 = Tex(r"= M\cdot\frac{1}{2}(x-a)^2\bigg|_a^b",font="SimSun",font_size=30, color=BLUE).next_to(T6,DOWN,aligned_edge=LEFT)        
    self.play(TransformFromCopy(T6,T7))        
    self.wait(3)        
    T8 = Tex(r"= M\cdot\frac{(b-a)^2}{2}",font="SimSun",font_size=30, color=YELLOW).next_to(T6,DOWN,aligned_edge=LEFT)        
    self.play(Transform(T7,T8))        
    self.wait(10)